/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.deployment.relatorio;

import br.com.deployment.daoImpl.QuestionnaireResponseDAOImpl;
import br.com.deployment.entities.QuestionnaireResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

/**
 *
 * @author ely
 */
@ManagedBean
@SessionScoped
public class ManagerRelCorreicao {
    
    @EJB
    QuestionnaireResponseDAOImpl qService;
    
    private List<QuestionnaireResponse> lstQuestionnaireResponse;
    
    private QuestionnaireResponse questionaire;
    
    
    public ManagerRelCorreicao(){
     questionaire = new QuestionnaireResponse();
    }
    
    public void generetorReportPdf(QuestionnaireResponse quesResponse) throws JRException, IOException {

        this.questionaire = qService.find(quesResponse.getId());
        
        HashMap<String, Object> h = new HashMap<String, Object>();
        String text = "";
        String cabecalho = "";
        String titular   = "";
//        SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
//        Date data = quesResponse.getDateQ(); 
//          
        cabecalho += "Município/Comarca : " +this.questionaire.getNotaryOffice().getAddress().getCity().getName()+"-"+this.questionaire.getNotaryOffice().getAddress().getCity().getStates().getSgl();
        cabecalho +="\n         Denominação : " +this.questionaire.getNotaryOffice().getDenominationOffice();
        cabecalho +="\n                 Natureza : " +this.questionaire.getNotaryOffice().getNatureOffice();
        cabecalho +="\n                Endereço : " +this.questionaire.getNotaryOffice().getAddress().getStreet();
        cabecalho +="\n                      Bairro : " +this.questionaire.getNotaryOffice().getAddress().getDistrict();
        cabecalho +="\n                         Cep : " +this.questionaire.getNotaryOffice().getAddress().getCep();
        
        titular +=" Nome / Delegatário : " +this.questionaire.getNotaryOffice().getTitular();
        titular +="\n                         CPF : " +this.questionaire.getNotaryOffice().getCpf();
        titular +="\nIgresso na Atividade : " +this.questionaire.getNotaryOffice().getAdmission();
         
         
        text +=" Ciente da Correição, pendências e conclusão";
                   
        h.put("cabecalho", cabecalho);
        h.put("titular", titular);
        h.put("texto", text);
        
        lstQuestionnaireResponse = qService.findQuestionnaireResponse(this.questionaire);
        JRBeanCollectionDataSource collecion = new JRBeanCollectionDataSource(lstQuestionnaireResponse);
        String reportPath = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/relatorio/relCorreicao.jasper");
        JasperPrint jasperPrint = JasperFillManager.fillReport(reportPath, h, collecion);
        HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
        httpServletResponse.addHeader("Content-disposition", "attachment; filename=edital.pdf");
        ServletOutputStream serveOutputStream = httpServletResponse.getOutputStream();
        JasperExportManager.exportReportToPdfStream(jasperPrint, serveOutputStream);
        FacesContext.getCurrentInstance().responseComplete();

    }

    public List<QuestionnaireResponse> getLstQuestionnaireResponse() {
        return lstQuestionnaireResponse;
    }

    public void setLstQuestionnaireResponse(List<QuestionnaireResponse> lstQuestionnaireResponse) {
        this.lstQuestionnaireResponse = lstQuestionnaireResponse;
    }

    public QuestionnaireResponse getQuestionaire() {
        return questionaire;
    }

    public void setQuestionaire(QuestionnaireResponse questionaire) {
        this.questionaire = questionaire;
    }

    
    
}
